package com.ygxsk.carhome.views.picker;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.blankj.utilcode.util.LogUtils;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.integrated.retrofit.Parameterkey;

import java.util.ArrayList;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-04-13  16:41
 * @Description: 普通的文字选择器,这个被设计成了通用的，需要通过indexFlag来标识
 */
public class NomalPickerDialogFragment extends DialogFragment {

    //当前选中的精确的标题名字 ：默认请选择
    private String chooseTitle = "";

    //该页面数据的标识状态；
    private int indexFlag;

    //月份的选择器具
    protected NomalCustomPicker mNomalPicker;

    //当前页面被选中的那个值，首次弹出的时候，这个值是默认的第一条值内容，：因为假如用户没有滚动视图，那么造成，选中是空；
    private NomalCustomBean mSelectedNomalValue = null;

    //这里选择月份的选择监听器
    private OnNomalChooseListener mOnNomalChooseListener;

    private boolean mIsShowAnimation = true;

    protected TextView mCancelButton, mDecideButton, mClearButton;

    public void setOnNomalChooseListener(OnNomalChooseListener onDateChooseListener) {
        mOnNomalChooseListener = onDateChooseListener;
    }

    public void showAnimation(boolean show) {
        mIsShowAnimation = show;
    }

    // TODO: 2023-08-05 15:33 将来要写成一个，暂时为了调试接口，屏蔽错误
    public static NomalPickerDialogFragment newInstance(ArrayList<NomalCustomBean> _dataResources) {

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "newInstance--newInstance--newInstance");

        Bundle args = new Bundle();

        NomalPickerDialogFragment fragment = new NomalPickerDialogFragment();

        // Supply num input as an argument.
        args.putParcelableArrayList(Parameterkey.data, _dataResources);
        //args.putInt(Parameterkey.indexFlag, _indexFlag);

        fragment.setArguments(args);

        return fragment;
    }

    /**
     * @param _dataResources 填充存储的资源列表
     * @param _indexFlag     标记是从什么模块页面过去的，然后取出数据的时候就从哪个页面取出；
     * @return
     */
    public static NomalPickerDialogFragment newInstance(String _chooseTitle, ArrayList<NomalCustomBean> _dataResources, int _indexFlag) {

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "newInstance--newInstance--newInstance--->" + _indexFlag);

        Bundle args = new Bundle();

        NomalPickerDialogFragment fragment = new NomalPickerDialogFragment();

        // Supply num input as an argument.
        args.putString(Parameterkey.chooseTitle, _chooseTitle);
        args.putParcelableArrayList(Parameterkey.data, _dataResources);
        args.putInt(Parameterkey.indexFlag, _indexFlag);

        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onCreate--onCreate--onCreate");

        if (getArguments() != null) {

//            mParam1 = getArguments().getString(ARG_PARAM1);
            chooseTitle = getArguments().getString(Parameterkey.chooseTitle);
            indexFlag = getArguments().getInt(Parameterkey.indexFlag);
            //这时候不用取出这一个NomalCustomPicker而可以直接赋值；
//            if (mNomalPicker!=null){
//                mNomalPicker.setIndexFlagWhere(indexFlag);
//            }else{
//                mNomalPicker = new NomalCustomPicker(this.getContext());
//                mNomalPicker.setIndexFlagWhere(indexFlag);
//            }

//            LogUtils.d(ConstantSign.LOGCAT_NOMAL + "indexFlag---onCreate-indexFlag---onCreate-indexFlag-->" + indexFlag);

        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onCreateView--onCreateView--onCreateView");
        View view = inflater.inflate(R.layout.dialognomalpicker, container);

        mNomalPicker = (NomalCustomPicker) view.findViewById(R.id.nomalpicker_dialog);
        mNomalPicker.setIndexFlagWhere(indexFlag);
        //mNomalPicker.setDataResources();
        //mYearPicker.setShowCurtainBorder(false);//不要粗体
        //mYearPicker.setCurtainBorderColor(R.color.com_ycuwq_datepicker_divider);//设置幕布间隔颜色

        //重置按钮
        mClearButton = (TextView) view.findViewById(R.id.btn_dialog_nomal_clear);
        mClearButton.setText(chooseTitle);

        //取消按钮
        mCancelButton = (TextView) view.findViewById(R.id.btn_dialog_year_cancel);
        //确认按钮
        mDecideButton = (TextView) view.findViewById(R.id.btn_dialog_year_decide);

        if (mNomalPicker != null) {
            mNomalPicker.setOnNomalSelectedListener(new NomalCustomPicker.OnNomalSelectedListener() {
                @Override
                public void onNomalSelected(int _indexNomal, NomalCustomBean _itemBean, String _nomalString) {

                    try {
                        //do
                        //选中的内容是
                        mSelectedNomalValue = _itemBean;

                        LogUtils.json(ConstantSign.LOGCAT_NOMAL + "滚轮选中的是", _itemBean);

                    } catch (Exception e) {
                        //catch
                        e.printStackTrace();
                    }
                }
            });
        }

        //重置清空
//        if (mClearButton != null) {
//            mClearButton.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    try {
//
//                        //do
//                        if (mOnNomalChooseListener != null) {
//                            mOnNomalChooseListener.onNomalChoose(null);
//                        }
//
//                    } catch (Exception e) {
//                        //catch
//                        e.printStackTrace();
//                    } finally {
//                        dismiss();
//                    }
//
//                }
//            });
//        }

        if (mCancelButton != null) {
            mCancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        if (mDecideButton != null) {
            mDecideButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {
                        //do
                        if (mOnNomalChooseListener != null) {
                            if (mSelectedNomalValue != null) {
                                mOnNomalChooseListener.onNomalChoose(mSelectedNomalValue);
                            } else {
                                //ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                                mSelectedNomalValue = mNomalPicker.getSelectedNomal();
                                //这时候不刷新，因为没有发生滚轮滚动
                                mOnNomalChooseListener.onNomalChoose(mSelectedNomalValue);
                                LogUtils.json(ConstantSign.LOGCAT_NOMAL + "走到了滚轮的值 是空，所以只能取出，默认弹开的第一个值--->", mSelectedNomalValue);
                            }
                        }

                    } catch (Exception e) {
                        //catch
                        e.printStackTrace();
                    }

                    dismiss();
                }
            });
        }


//        if (mSelectedMonth > 0) {
//            setSelectedDate();
//        }

        initChild();

        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity(), R.style.DatePickerBottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定

        dialog.setContentView(R.layout.dialognomalpicker);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消

        Window window = dialog.getWindow();

        if (window != null) {
            if (mIsShowAnimation) {
                window.getAttributes().windowAnimations = R.style.DatePickerDialogAnim;
            }
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.BOTTOM; // 紧贴底部
            lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
            lp.dimAmount = 0.35f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }

        return dialog;
    }

    protected void initChild() {

        //

    }

    public interface OnNomalChooseListener {
        void onNomalChoose(NomalCustomBean nomalCustomBean);
    }

}
